package ch.fhnw.ams.sudoku.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.fhnw.ams.sudoku.Globals;

/**
 * Shows the software version and the link to the project page. 
 * 
 * @author amsler
 *
 */
public class InfoDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	
	
	/**
	 * Creates a new info dialog
	 */
	public InfoDialog(JFrame parent) {
		super(parent, Globals.APP_NAME + " " + Globals.APP_VERSION);
		
		JPanel infoPanel = getInfoPanel();
		getContentPane().add(infoPanel, BorderLayout.CENTER);
		getContentPane().setBackground(Color.WHITE);
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setFocusable(false);
		setResizable(false);
		pack();
		centerDialog();
		setVisible(true);
	}

	private JPanel getInfoPanel() {
		JPanel infoPanel = new JPanel();
		infoPanel.setOpaque(false);
		
		GridBagLayout gbLayout = new GridBagLayout();
		infoPanel.setLayout(gbLayout);
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		JLabel sidekickLabel = new JLabel(new ImageIcon(Globals.SIDEKICK_IMAGE_URL));
		gbc.gridy = 0;
		gbc.insets = new Insets(10, 10, 10, 10);
		infoPanel.add(sidekickLabel, gbc);
		
		JLabel versionLabel = new JLabel("Version " + Globals.APP_VERSION);
		gbc.gridy = 1;
		gbc.insets = new Insets(2, 10, 2, 10);
		infoPanel.add(versionLabel, gbc);
		
		JLabel autorLabel = new JLabel(Globals.MSG.getString("autor.info"));
		gbc.gridy = 2;
		gbc.insets = new Insets(2, 10, 2, 10);
		infoPanel.add(autorLabel, gbc);
		
		JLabel buildInfo = new JLabel("Built with Java " + Globals.JAVA_VERSION);
		gbc.gridy = 3;
		gbc.insets = new Insets(2, 10, 2, 10);
		infoPanel.add(buildInfo, gbc);
		
		JLabel homepage = new JLabel(Globals.URL);
		gbc.gridy = 4;
		gbc.insets = new Insets(2, 10, 10, 10);
		infoPanel.add(homepage, gbc);
		
		return infoPanel;
	}
	
	private void centerDialog() {
		Container container = getParent();
		if (!(container instanceof JFrame)) return;
		JFrame frame = (JFrame) container;
		
		Point frameLocation = frame.getLocation();
		Dimension frameDimension = frame.getSize();
		
		int x = (frameDimension.width - getSize().width) / 2;
		int y = (frameDimension.height - getSize().height) / 2;
		
		setLocation(frameLocation.x + x, frameLocation.y + y);
	}
	
}
